package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.RecommendUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;
    //查询今日佳人
    @Override
    public RecommendUser findTodayBest(Long toUserId) {
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId) //推荐给谁
        ).with(Sort.by(Sort.Order.desc("score")))//分数排序
                .skip(0).limit(1);//查询一条
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    //推荐列表
    @Override
    public PageBeanVo findRecommedUserByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        int startIndex = (pageNum-1)*pageSize;
        if(pageNum==1){
            startIndex = startIndex+1; //跳过第一条
        }

        Query query = new Query(
                Criteria.where("toUserId").is(toUserId) //推荐给谁
        ).with(Sort.by(Sort.Order.desc("score")))//分数排序
                .skip(startIndex).limit(pageSize);//查询一条
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class); //查询数据

        long count = mongoTemplate.count(query, RecommendUser.class);//查询总记录数

        return new PageBeanVo( pageNum , pageSize , count ,recommendUserList );
    }

    //查询两个人的缘分值
    @Override
    public RecommendUser findByJiarenIdAndToUserId(Long jiarenId, Long toUserId) {

        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)//我的id
                .and("userId").is(jiarenId) //对方的id
        );
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }
}
